<template>
  <div class="box">
    <el-card class="box-card">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        label-width="120px"
        class="demo-ruleForm"
        status-icon
      >
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="上级菜单" prop="region">
          <el-select v-model="ruleForm.parentId" placeholder="无上级菜单">
            <el-option label="无上级菜单" :value="0" />
            <!-- <el-option v-show="levelyy == 0 ? false : true" label="权限" value="" />
                        <el-option v-show="levelyy == 0 ? false : true" label="商品" value="" />
                        <el-option v-show="levelyy == 0 ? false : true" label="订单" value="" />
                        <el-option v-show="levelyy == 0 ? false : true" label="营销" value="" />
                        <el-option v-show="levelyy == 0 ? false : true" label="其他" value="" /> -->
          </el-select>
        </el-form-item>
        <el-form-item label="前端名称" prop="name1">
          <el-input
            :disabled="levelyy == 1 ? false : true"
            v-model="ruleForm.name"
          />
        </el-form-item>
        <el-form-item label="前端图标" prop="name2">
          <el-input
            :disabled="levelyy == 1 ? false : true"
            v-model="ruleForm.icon"
          />
        </el-form-item>
        <el-form-item label="是否显示" prop="resource">
          <el-radio-group v-model="ruleForm.hidden">
            <template #default="scope">
              <el-radio :label="0">是</el-radio>
              <el-radio :label="1">否</el-radio>
            </template>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="name">
          <el-input v-model="ruleForm.sort" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="hdtijiao"> 提交 </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import * as api from "../../request/api";
import router from "../../router";
import { useRoute } from "vue-router";
import type { adminCreateItem } from "../../request/UmsMenuController/index";
import type { Ref } from "vue";
// 添加
// let modalForm = ref({}) as Ref<IAdminListItem>
const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  title: "",
  name: "",
  icon: "",
  sort: "",
  hidden: 0,
  parentId: 0,
});

let levelyy = ref(1);
// 监听
const route = useRoute();
console.log(route);
watch(
  () => route.query.id,
  async (newId) => {
    // 发起请求
    if (route.query.id) {
      console.log(`${newId}`);
      // getAdminID
      let idtaozhuan = await api.menu.getAdminID(route.query.id);
      // console.log("idtaozhuan", idtaozhuan);
      ruleForm.title = idtaozhuan.title;
      ruleForm.name = idtaozhuan.name;
      ruleForm.icon = idtaozhuan.icon;
      ruleForm.sort = idtaozhuan.sort;
      ruleForm.hidden = idtaozhuan.hidden;
      levelyy = idtaozhuan.level;
    }
  },
  { immediate: true }
);
// 创建一个空的对象
let newData = ref({}) as Ref<adminCreateItem>;
// 提交
let hdtijiao = async () => {
  let haha = ruleForm;
  // adminCreate
  let tijiaodata = await api.menu.adminCreate(haha);
  // console.log("tijiaodata", tijiaodata);
  router.push("/ums/menu");

  setTimeout(() => {
    router.go(0);
  }, 0);
};
</script>

<style lang="scss" scoped>
.box {
  margin: 20px;
}
</style>
